"""Redis连接管理."""

import redis.asyncio as redis
from redis.asyncio import Redis

from netsphere.config.settings import settings

# 创建Redis连接池
redis_pool = redis.ConnectionPool(
    host=settings.redis.host,
    port=settings.redis.port,
    db=settings.redis.db,
    password=settings.redis.password,
    max_connections=settings.redis.max_connections,
    socket_timeout=settings.redis.socket_timeout,
    socket_connect_timeout=settings.redis.socket_connect_timeout,
    decode_responses=True,
)

# 创建Redis客户端
redis_client: Redis = redis.Redis(connection_pool=redis_pool)


async def get_redis() -> Redis:
    """获取Redis客户端."""
    return redis_client


async def close_redis() -> None:
    """关闭Redis连接."""
    await redis_client.close()
    await redis_pool.disconnect()